GBML manual / Arrays

Arrays

array is the plainest data container - a fixed number of slots, indexed by position, that can never grow or shrink once declared.

What makes an array different

array<T> is the simplest of the seven containers on this manual, and the one every other one is measured against: a fixed number of slots, of one element type, indexed by position. Nothing about it can ever grow or shrink - and that is the whole point of reaching for one. You decide how many slots it has when you declare it, and that number is the number of slots it has. There is no "empty vs filled" idea the way list<T> has one. In that sense array<T> is grid<T>'s one-dimensional twin: both are fixed blocks of slots reached only by indexing, never by an add/remove function.

array<T> has ten functions of its own on this manual:

FunctionWhat it doesIt gives back
array_create(size, fill)makes an array with every slot already filledthe new array
array_length(array)counts the places the array hasthe count
array_copy(array)makes a second array just like itthe new array
array_contains(array, value)looks for valuetrue or false
array_find(array, value)looks for value and says where it isits place, or -1 if it is not there
array_reverse(array)turns the order of the values aroundnothing
array_sort(array, ascending)puts the numbers or the text in order, smallest (A) first or biggest (Z) firstnothing
array_shuffle(array)mixes up the order of the valuesnothing
array_foreach(array, script)runs one of your scripts once for each placenothing
array_filter(array, script)makes a list of just the values a script says yes tothe new list

Nothing can change how many places an array has once it is declared, so there is no function to add or remove one. That is also why array_filter gives back a list and not an array: how many values pass is only known while the game runs. Reading and writing a slot (below) is the rest of the interface. array_sort, array_shuffle and array_reverse rearrange the array itself without adding or removing a place, and make no copy. array_find gives back -1 when the value is not in the array, so test it with >= 0 before using it as a place.

array_length answers with the places the array really has, which is the size you wrote down - except for the short-literal case described under "Declaring one" below, where it tells you the true, smaller number. Inside a script it is the only way to learn how long the caller's array is. array_copy makes a whole second array of the same size, so it costs a second array's worth of memory.

Declaring one

The shortest way is to write nothing special at all. An array needs no tag and no size: give it a list of values and it holds exactly those, in a container that has room for 8 (the same default every container on this manual starts with):

var scores = [10, 20, 30];

That is a complete, working declaration. Both tags below are optional, and you only reach for them when the plain version is not enough.

Here they are together, which is the heaviest an array declaration gets:

/// @type {array<int>}
/// @capacity 5
var scores = [0, 0, 0, 0, 0];

A /// tag is not the only way to say this. Every one of these jobs can also be written as ordinary code, with no comment involved - both ways are fully supported and neither is going away. For an array:

var scores = array_create(5, 0);          // five slots, all 0 - the type comes from the 0
var flags: array<bool, 12> = [];          // the type and the size, after the name

array_create makes an array of a set size with every slot already filled, which is the one job the tags could only do by making you type every starting value out by hand. The : array<bool, 12> form sets the size inside the type. Writing a type out lists every tag next to its written-out equivalent in one table.

Watch the difference between the size and the number of values. The size (5) is how much room the container has, but an array literal only puts in the values you actually wrote. /// @capacity 5 with a 3-element literal ([10, 20, 30]) is legal - the compiler only ever objects when a literal has more elements than the size, never fewer - but the array then holds just those three values, not five. The other two slots are not zeros waiting to be used. They do not exist. Reading or writing scores[3] or scores[4] is a mistake that the compiler does not catch. The Game Boy Advance module, which the game is built on, checks every index while the game runs and stops the game with an "Invalid index" message. array_length(scores) says 3 for that array, not 5: it counts the places that exist, so a loop that stops at it never reaches the missing two. And since an array<T> has no list_add, nothing can ever add those slots later - the room you asked for is simply wasted. When every slot must exist from the start, write them all out or use array_create(5, 0), which always fills all five. When you want a container that starts short and fills up as the game goes on, that is a list<T>. See Variables, types and scope for the full /// @capacity tag and its default, shared by every container on this manual.

Reading and writing a slot

Plain [i] reads or writes one slot, zero-based, on either side of = - and it works from the first frame, on every slot, since there is no "not added yet" state to run into:

scores[2] = 99;
var third = scores[2]; // 99

See Accessors for how this same [i] syntax behaves across every container that offers it.

array<T> or list<T>?

This is the one real decision array<T> asks you to make, and it is worth making on purpose rather than by habit. array<T> and list<T> are stored exactly the same way underneath - the only difference is which operations the compiler lets you perform on one. list<T> allows everything array<T> does, plus list_add/list_insert/list_remove/ list_clear, the four that change how many items there are - reach for array<T> only when you are certain you will never want any of those four. (Searching and turning the order around are not on that list: an array has array_contains, array_find and array_reverse too.)

Try one of them on a real array<T> and the compiler refuses it outright, by name, before the game ever runs:

Argument 1 to `list_add` needs a list<T>, but this is an array<int> - array<T> has a fixed size.

The fix is exactly what it sounds like, and exactly one word: change the tag from array<int> to list<int>. Nothing else about the code moves - the same [i], the same storage, the same default capacity rule - because list<T> really is just array<T> with the add, insert, remove and clear functions unlocked.

Reach for array<T> when the count is fixed and you already know it: five equipment slots that always exist whether or not each one is currently in use, the four cardinal directions' worth of a lookup table, a level's fixed set of checkpoints. Reach for list<T> the moment anything needs to come or go - a spawned-enemies list, a collected-pickups history, anything whose count only becomes known while the game is running.

Example

/// @type {array<int>}
/// @capacity 5
var scores = [10, 20, 30, 40, 50];
scores[2] = 99;
var third = scores[2]; // 99

The same array, written without the tags:

var scores = array_create(5, 0);
scores[2] = 99;
var third = scores[2]; // 99

See also

Data containers covers where array<T> sits next to the other six, and the rules every container shares. Accessors covers [i] indexing in full, across every container that offers it.

See also: Data containersAccessorsarray_createarray_lengtharray_copyarray_containsarray_findarray_reversearray_sortarray_shufflearray_foreacharray_filterlist_addGridsVariables, types and scopeWriting a type out, instead of in a comment